package mySpringMVC;

import javaBean.Employee;
import javaBean.News;
import myConnection.DBConn;
import myConnection.Modify;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class MyController {

    //登录验证
    @RequestMapping("/login")
    public ModelAndView checkLogin(HttpServletRequest request){
        HttpSession session =  request.getSession();
        Employee employee = new Employee();
        employee.setName(request.getParameter("name"));
        employee.setPassword(request.getParameter("password"));
        Boolean isTrue = DBConn.check(employee);
        session.setAttribute("isTrue",isTrue);
        session.setAttribute("userName",request.getParameter("name"));
        if (isTrue)
            return setIndex();
        else
            return new ModelAndView("login");
    }

    //注销
    @RequestMapping("/loginOut")
    public ModelAndView loginOut(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("isTrue",false);
        return setIndex();
    }

    //点击单一新闻的请求响应
    @RequestMapping("/info")
    public ModelAndView dispatcherInfo(@RequestParam("type") String type,
                                       @RequestParam("id") String id){
        News news = null;
        if ("imagenews".equals(type)){
            news =  DBConn.getNewsById(type,Integer.parseInt(id));
        }
        else if ("academiccommunication".equals(type)){
            news =  DBConn.getNewsById(type,Integer.parseInt(id));
        }
        else if ("couriernews".equals(type)){
            news =  DBConn.getNewsById(type,Integer.parseInt(id));
        }
        else if ("dynamicofparty".equals(type)){
            news =  DBConn.getNewsById(type,Integer.parseInt(id));
        }
        else if ("notifydynamic".equals(type)){
            news =  DBConn.getNewsById(type,Integer.parseInt(id));
        }
        else if ("projectlist".equals(type)){
            news =  DBConn.getNewsById(type,Integer.parseInt(id));
        }
        return getModel(news);
    }

    //设置网站的访问路径和全部新闻的显示,以及返回按钮访问新闻主页
    @RequestMapping(value = {"/index","/back"})
    public ModelAndView setIndex(){
        ModelAndView modelAndView = new ModelAndView("index");
        modelAndView.addObject("imagenewsList",DBConn.getNews("imagenews"));
        modelAndView.addObject("academicList",DBConn.getNews("academiccommunication"));
        modelAndView.addObject("couriernewsList",DBConn.getNews("couriernews"));
        modelAndView.addObject("dynamicofpartyList",DBConn.getNews("dynamicofparty"));
        modelAndView.addObject("notifydynamicList",DBConn.getNews("notifydynamic"));
        modelAndView.addObject("projectList",DBConn.getNews("projectlist"));
        return modelAndView;
    }

    //显示该类型的所有新闻
    @RequestMapping("/newsPublish")
    public ModelAndView getTypeNews(@RequestParam("type") String databaseName){
        ModelAndView modelAndView = new ModelAndView("newsPublish");
        List<News> list = DBConn.getAllNews(databaseName);
        modelAndView.addObject("list",list);
        modelAndView.addObject("name",databaseName);
        return modelAndView;
    }


    //设置重定向的视图和数据
    public ModelAndView getModel(News news){
        ModelAndView modelAndView = new ModelAndView("info");
        modelAndView.addObject("oneNews",news);
        return modelAndView;
    }

    //新闻的修改
    @RequestMapping("/upData")
    public ModelAndView upData(@RequestParam("type") String databaseName, @RequestParam("id") int id){
        ModelAndView modelAndView = new ModelAndView("addNews");
        //数据库获取待修改的新闻信息
        News news =  DBConn.getNewsById(databaseName,id);
        //将数据发送至新闻编辑页面进行填充
        modelAndView.addObject("updata",news);
        modelAndView.addObject("name",databaseName);
        //设置修改操作的标识
        Modify.modify = true;
        //设置修改的新闻id
        Modify.modifyId = id;
        return modelAndView;
    }

    //添加新闻
    @RequestMapping("AddNews")
    public ModelAndView addNews(@RequestParam("title") String title, @RequestParam("type") String type,
                                @RequestParam("content") String content){

        News news = new News();
        news.setTitle(title);
        news.setContent(content);
        news.setTime(new Date());

        if ("图片新闻".equals(type))
            type = "imagenews";
        else if ("学术交流".equals(type))
            type = "academiccommunication";
        else if ("新闻速递".equals(type))
            type = "couriernews";
        else if ("党建动态".equals(type))
            type = "dynamicofparty";
        else if ("通知公告".equals(type))
            type = "notifydynamic";
        else if ("专题列表".equals(type))
            type = "projectlist";
        else
            type  = "imagenews";

        if (!Modify.modify){
            //向数据库写入数据
            DBConn.insertNews(news,type);
        }
        else{
            news.setId(Modify.modifyId);
            DBConn.upDataNewsById(news,type);
            //重置Modify为默认状态
            Modify.modify = false;
            Modify.modifyId = -1;
        }
        return getTypeNews(type);
    }

    //添加新闻按钮事件
    @RequestMapping("addNews")
    public String add(){
        return "addNews";
    }

    //删除新闻
    @RequestMapping("/delete")
    public ModelAndView deleteNews(@RequestParam("type") String type, @RequestParam("id") int id){
        DBConn.deleteNews(type,id);
        return getTypeNews(type);
    }
}
